/**
 * 
 */
package com.gopay.bis.manager.bank.ebank;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

import javax.annotation.Resource;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.psbc.ebank.PsbcB2cMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

/**
 * @PsbcB2cManager.java
 * @author Ryan
 * @2013-12-2 上午10:56:34 www.gopay.com.cn Inc.All rights reserved.
 */
public class PsbcB2cManager implements BankManagerRemoteService {

	@Resource(name = "psbcB2cBankServiceClientHna")
	private BankService psbcB2cBankServiceClientHna;

	@Resource(name = "psbcB2cBankServiceClient")
	private BankService psbcB2cBankServiceClient;
	
	@Resource(name = "psbcConfig")
	private Map<String, String> psbcConfig;

	@Override
	public Object sign(BaseMessage message) throws GopayException {
		PsbcB2cMessage psbcB2cMessage = (PsbcB2cMessage) message;
		psbcB2cMessage.setBankUrl(psbcB2cMessage.getPayUrl()); // 请求银行URL
		psbcB2cMessage.setTranAbbr(psbcConfig.get("PSBC.B2C.TRANABBR"));
		psbcB2cMessage.setMercCode(psbcB2cMessage.getMerID());
		String merUrl = psbcB2cMessage.getPayRuturnUrl();
		try {
			psbcB2cMessage.setMercUrl(URLEncoder.encode(merUrl, "utf-8"));
		} catch (UnsupportedEncodingException e) {
			throw new GopayException("", e);
		}
		psbcB2cMessage.setRemark1(psbcConfig.get("PSBC.B2C.REMARK1"));
		psbcB2cMessage.setPlainMessage(buildPsbcB2c(psbcB2cMessage));
		BankLogUtils.printB2CManagerSignSendLog(psbcB2cMessage);
		String signStr;
		if(psbcB2cMessage.isHna()){
			signStr = (String) psbcB2cBankServiceClientHna.sign(psbcB2cMessage);
		}else{
			signStr = (String) psbcB2cBankServiceClient.sign(psbcB2cMessage);
		}
		psbcB2cMessage.setSignMessage(signStr);
		BankLogUtils.printB2CManagerSignReturnLog(message);
		return psbcB2cMessage;
	}

	@Override
	public Object verify(BaseMessage message) throws GopayException {
		PsbcB2cMessage psbcB2cMessage = (PsbcB2cMessage) message;
		BankLogUtils.printB2CManagerVerifySendLog(psbcB2cMessage);
		boolean isVerify;
		if(psbcB2cMessage.isHna()){
			isVerify = (Boolean) psbcB2cBankServiceClientHna.verify(psbcB2cMessage);
		}else{
			isVerify = (Boolean) psbcB2cBankServiceClient.verify(psbcB2cMessage);
		}
		BankLogUtils.printB2CManagerVerifyReturnLog(psbcB2cMessage, isVerify);
		return isVerify;
	}

	@Override
	public Object query(Object obj) throws GopayException {
		PsbcB2cMessage psbcQueryMessage = (PsbcB2cMessage) obj;
		if(BisPayCheckUtils.checkExternalOrderID(psbcQueryMessage.getOrderId())){
			psbcQueryMessage.setTermSsn(psbcQueryMessage.getOrderId());
		}
		psbcQueryMessage.setTranAbbr(psbcConfig.get("PSBC.B2C.QUERY.TRANSNAME"));
		psbcQueryMessage.setoTranAbbr(psbcConfig.get("PSBC.B2C.TRANABBR"));
		psbcQueryMessage.setMercCode(psbcQueryMessage.getMerID());
		
        if (BisBankBusinessProd._11.value.equals(psbcQueryMessage.getBankBusProd().value)) {
            // wap网关交易码
            psbcQueryMessage.setoTranAbbr(psbcConfig.get("PSBC.B2C.WAP.TRANABBR"));
        }
		psbcQueryMessage.setPlainMessage(buildQueryPlain(psbcQueryMessage));
		BankLogUtils.printB2CManagerQuerySendLog(psbcQueryMessage,psbcQueryMessage);
		PsbcB2cMessage result;
		if(psbcQueryMessage.isHna()){
			result = (PsbcB2cMessage) psbcB2cBankServiceClientHna.query(psbcQueryMessage);
		}else{
			result = (PsbcB2cMessage) psbcB2cBankServiceClient.query(psbcQueryMessage);
		}
		result.setOutStlmCd(BankConstants.BANK_LOGO_PSBC);
		BankLogUtils.printB2CManagerQueryReturnLog(result);
		return result;
	}

	/**
	 * 构建B2C签名明文
	 * 
	 * @param message
	 * @return
	 */
	private String buildPsbcB2c(PsbcB2cMessage message) {
		StringBuilder sb = new StringBuilder("");
		sb.append("TranAbbr=").append(message.getTranAbbr());
		sb.append("|MercDtTm=").append(message.getOrderDate());
		sb.append("|TermSsn=").append(message.getOrderId());
		sb.append("|OSttDate=").append(message.getoSttDate());
		sb.append("|OAcqSsn=").append(message.getoAcqSsn());
		sb.append("|MercCode=").append(message.getMercCode());
		sb.append("|TermCode=").append(message.getTermCode());
		sb.append("|TranAmt=").append(message.getAmount().toString());
		sb.append("|MercUrl=").append(message.getMercUrl());
		sb.append("|Remark1=").append(message.getRemark1());
		sb.append("|Remark2=").append(message.getRemark2());
		return sb.toString();
	}

	/**
	 * 构建订单查询明文
	 * 
	 * @param message
	 * @return
	 */
	private String buildQueryPlain(PsbcB2cMessage message) {
		StringBuffer sf = new StringBuffer("");
		sf.append("OTranAbbr=").append(message.getoTranAbbr());
		sf.append("|MercCode=").append(message.getMercCode());
		sf.append("|TermSsn=").append(message.getTermSsn());
		return sf.toString();
	}

	@Override
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(BaseMessage base) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

}
